﻿Imports ccapps.persistent.ChangeSIMpersistence
Public Class ChangeSIMScreen

    Inherits System.Web.UI.Page



    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Session("user") = "" Or Session("user") Is Nothing Then
            ' Response.Redirect("../../login.aspx")
            Session("message") = "!!Session Timeout!!,Tekrar Giriş Yapmalısınız"
            Response.Clear()
            Response.Status = "302 Object Moved"
            Response.RedirectLocation = "CX_Exception.aspx"
            Response.End()
        End If

        If Not IsPostBack Then
            GuncelBilgi.Visible = False
            YeniSimTable.Visible = False
            GuncelBilgi.Visible = False
            YeniSimTable.Visible = False
        End If

       
       


    End Sub

    
   
    Protected Sub btnCOID_Click(sender As Object, e As EventArgs) Handles btnCOID.Click
        TB_MSISDN.Text = ""
        Clear()
        GuncelBilgi.Visible = True
        YeniSimTable.Visible = True
        DG_SimHistoryTable.DataSource = Nothing
        DG_SimHistoryTable.DataBind()
        CustcodeSearch()
        GetDisplaySIMHistory()
        DisplaySIMChangeReason()
    End Sub

    Private Sub CustcodeSearch()

        Try
            Dim pin_COID As Integer = Convert.ToInt32(TB_COID.Text)
            Dim dt As DataTable = GetCustDealerInfoWithCOID(pin_COID)


            If dt.Rows.Count > 0 Then
                For Each dr2 As DataRow In dt.Rows

                    lblAbone.Text = dr2("ccfname").ToString() + " " + dr2("cclname").ToString() + "," + dr2("CCCITY").ToString()
                    lblGuncelSim.Text = dr2("DN_NUM").ToString() + "," + dr2("SM_SERIALNUM").ToString()
                    hiddenlblGuncelSim.Text = dr2("SM_SERIALNUM").ToString()
                    lblGuncelSatisOrtagi.Text = dr2("DEALER_ID").ToString() + dr2("dealer_fname").ToString() + "," + dr2("dealer_lname").ToString() + "," + dr2("dealer_city").ToString()
                    lblhidCO_ID.Text = dr2("CO_ID").ToString()
                Next
            Else
                LblErrorMessage.Text = "Girmiş Olduğunuz COID'e Ait Bilgi Yoktur."
            End If
        Catch ex As Exception
            LblErrorMessage.Text = "COID numarası hatalı girilmiştir. Sistemde böyle bir COID bulunmuyor!"
        End Try
       
    End Sub

    Protected Sub btnMSISDN_Click(sender As Object, e As EventArgs) Handles btnMSISDN.Click
        Clear()
        TB_COID.Text = ""
        GuncelBilgi.Visible = True
        YeniSimTable.Visible = True
        DG_SimHistoryTable.DataSource = Nothing
        DG_SimHistoryTable.DataBind()
        MSISDNSearch()
        GetDisplaySIMHistory()
        DisplaySIMChangeReason()
    End Sub


    Private Sub MSISDNSearch()
        Try
            Dim pin_MSISDN As String = TB_MSISDN.Text.Trim()
            Dim dt As DataTable = GetCustDealerInfoWithMSISDN(pin_MSISDN)


            If dt.Rows.Count > 0 Then
                For Each dr2 As DataRow In dt.Rows

                    lblAbone.Text = dr2("ccfname").ToString() + " " + dr2("cclname").ToString() + "," + dr2("CCCITY").ToString()
                    lblGuncelSim.Text = dr2("DN_NUM").ToString() + "," + dr2("SM_SERIALNUM").ToString()
                    lblGuncelSatisOrtagi.Text = dr2("DEALER_ID").ToString() + dr2("dealer_fname").ToString() + "," + dr2("dealer_lname").ToString() + "," + dr2("dealer_city").ToString()
                    lblhidCO_ID.Text = dr2("CO_ID").ToString()
                    hiddenlblGuncelSim.Text = dr2("SM_SERIALNUM").ToString()

                Next
            Else
                LblErrorMessage.Text = "Girmiş Olduğunuz MSSIDN'e Ait Bilgi Yoktur."


            End If
        Catch ex As Exception
            LblErrorMessage.Text = "MSSIDN numarası hatalı girilmiştir. Sistemde böyle bir MSSIDN bulunmuyor!"

        End Try
        
    End Sub

    Private Sub GetDisplaySIMHistory()
        Try
            Dim pin_CO_ID As Integer = Convert.ToInt32(lblhidCO_ID.Text)
            Dim dt As DataTable = DisplaySimHistory(pin_CO_ID, Session("user"))
            'Response.Write(dt.Rows.Count)
            GridView2.DataSource = dt
            GridView2.DataBind()
        Catch ex As Exception
            LblErrorMessage.Text = ex.Message
        End Try

    End Sub

    Private Sub DisplaySIMChangeReason()

        Try
            DDL_ChangeReason.Items.Clear()
            Dim dt As DataTable = GetSIMChangeReason()


            If dt.Rows.Count > 0 Then
                For Each dr2 As DataRow In dt.Rows


                    DDL_ChangeReason.Items.Add(New ListItem(dr2("RS_DESC").ToString(), dr2("RS_ID").ToString()))


                Next

            End If
        Catch ex As Exception
            LblErrorMessage.Text = "Sim Geçmişi Görüntülenirken Hata Oluştu."
        End Try
      
    End Sub


    Protected Sub btnGonder_Click(sender As Object, e As EventArgs) Handles btnGonder.Click
        Try
            YeniSimTable.Visible = True
            GuncelBilgi.Visible = True

            ' Try
            Dim res_type As Integer = 2 ' 1=port 2=SIM Card
            Dim old_res_num As String = hiddenlblGuncelSim.Text
            Dim res_id As Integer
            Dim hlcode As Integer
            Dim smc_id As Integer
            Dim reason As Integer = Convert.ToInt32(DDL_ChangeReason.SelectedValue)
            Dim retention As Integer = Convert.ToInt32(DDL_TekrarKullanım.SelectedValue)
            Dim co_id As Integer = Convert.ToInt32(lblhidCO_ID.Text)
            Dim new_dealer_custcode As String = TB_YeniSatisOrtagi.Text

            Dim dt As DataTable = GetResIdForNewSim(TB_YeniSIM.Text)
            If dt.Rows.Count > 0 Then
                For Each dr2 As DataRow In dt.Rows
                    res_id = dr2("SM_ID").ToString
                    hlcode = dr2("HLCODE").ToString
                    smc_id = dr2("SMC_ID").ToString
                Next
            End If

            Dim sonuc As DataTable

            sonuc = ChangeSIMCard(res_type, old_res_num, res_id, hlcode, smc_id, reason, retention, co_id, new_dealer_custcode, Session("user"))
            GuncelBilgi.Visible = True
            YeniSimTable.Visible = True
            If sonuc.Rows.Count > 0 Then
                For Each dr2 As DataRow In sonuc.Rows

                    If dr2("result").ToString() = "-1" Then
                        cevap.Text = "Simkart atanamamıştır. Simkart başka bir müşteriye atanmış olabilir."
                    Else
                        cevap.Text = "İşlem başırıyla gerçekleştirlimiştir."
                    End If



                Next

            End If
            GetDisplaySIMHistory()
        Catch ex As Exception
            LblErrorMessage.Text = ex.Message
        End Try

       
    End Sub

    Protected Sub TB_YeniSatisOrtagi_TextChanged(sender As Object, e As EventArgs) Handles TB_YeniSatisOrtagi.TextChanged
        GuncelBilgi.Visible = True
        YeniSimTable.Visible = True
        Dim dt As DataTable = DealerControl(TB_YeniSatisOrtagi.Text)
        'Label5.Text += dt.Rows(0)(0).ToString() 
        If dt.Rows.Count > 0 Then
            If Convert.ToInt32(dt.Rows(0)(0).ToString()) = 0 Then
                Me.Page.RegisterStartupScript("javascript", "<script language=""javascript"">alert('Hatalı Dealer.');</script>")
                TB_YeniSatisOrtagi.Text = ""
            End If
        Else
            Me.Page.RegisterStartupScript("javascript", "<script language=""javascript"">alert('Hatalı Dealer.');</script>")
            TB_YeniSatisOrtagi.Text = ""
        End If
    End Sub

    Private Sub Clear()
        LblErrorMessage.Text = ""
        lblAbone.Text = ""
        lblGuncelSatisOrtagi.Text = ""
        lblGuncelSim.Text = ""
        TB_YeniSIM.Text = ""
        TB_YeniSatisOrtagi.Text = ""
        cevap.Text = ""
        DG_SimHistoryTable.DataSource = Nothing
        DG_SimHistoryTable.DataBind()
    End Sub


    Protected Sub GridView2_PageIndexChanging(sender As Object, e As GridViewPageEventArgs) Handles GridView2.PageIndexChanging
        GridView2.PageIndex = e.NewPageIndex
        GetDisplaySIMHistory()



    End Sub


    'Protected Sub GridView2_RowDataBound(sender As Object, e As GridViewRowEventArgs)

    '    Dim Dealername As Label = e.Row.FindControl("Label3")
    '    Dim DealerCity As Label = e.Row.FindControl("Label4")
    '    'Dim img As System.Drawing.Image = e.Row.FindControl("")


    '    Dim rowView As DataRowView = CType(e.Row.DataItem, DataRowView)

    '    'Dim imagePath As String = rowView("image_path")





    '    'Dim deneme3 As Label = e.Row.FindControl("Label5")
    '    'Dim deneme4 As Label = e.Row.FindControl("Label6")
    '    'Dim deneme5 As Label = e.Row.FindControl("Label7")
    '    'Dim deneme6 As Label = e.Row.FindControl("Label8")
    '    ''Dim deneme7 As Label = e.Row.FindControl("Label9")



    '    'If e.Row.RowType = DataControlRowType.DataRow Then
    '    '    Try
    '    '        Dim id As Integer = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "dealer_id").ToString())
    '    '        Dim dt As DataTable = New DataTable()
    '    '        dt = GetDealeridFromCustomerid(id)
    '    '        Dealername.Text = dt.Rows(0)("dealer_fname").ToString() + "  " + dt.Rows(0)("dealer_lname").ToString()
    '    '        DealerCity.Text = dt.Rows(0)("dealer_city").ToString()
    '    '    Catch ex As Exception
    '    '        Dealername.Text = "--"
    '    '        DealerCity.Text = "--"


    '    '    End Try








    '    'deneme.Text = Convert.ToInt32(LblCoid.Text).ToString() + "  " + id.ToString() + " " + Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "PROFILEID").ToString()).ToString()







    '    'End If
    'End Sub

    Protected Sub GridView2_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView2.RowDataBound
        Dim Dealername As Label = e.Row.FindControl("Label3")
        Dim DealerCity As Label = e.Row.FindControl("Label4")
        Dim UserName As Label = e.Row.FindControl("Label5")

        If e.Row.RowType = DataControlRowType.DataRow Then
            Try
                Dim id As Integer = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "dealer_id").ToString())
                Dim dt As DataTable = New DataTable()
                dt = GetDealeridFromCustomerid(id)
                If dt.Rows.Count > 0 Then
                    Dealername.Text = dt.Rows(0)("dealer_fname").ToString() + "  " + dt.Rows(0)("dealer_lname").ToString()
                    DealerCity.Text = dt.Rows(0)("dealer_city").ToString()
                Else
                    Dealername.Text = "--"
                    DealerCity.Text = "--"
                End If

               
            Catch ex As Exception
                Dealername.Text = "--"
                DealerCity.Text = "--"
            End Try

            Try
                Dim id2 As String = DataBinder.Eval(e.Row.DataItem, "CD_USERLASTMOD").ToString()
                Dim dt2 As DataTable = New DataTable()
                dt2 = UserInfo(id2)
                If dt2.Rows.Count > 0 Then
                    UserName.Text = dt2.Rows(0)("username").ToString()
                Else

                    UserName.Text = "--"

                End If

            Catch ex As Exception

            End Try
        End If

        'If e.Row.RowType = DataControlRowType.DataRow Then
        '    '    Try
        '    '        Dim id As Integer = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "dealer_id").ToString())
        '    '        Dim dt As DataTable = New DataTable()
        '    '        dt = GetDealeridFromCustomerid(id)
        '    '        Dealername.Text = dt.Rows(0)("dealer_fname").ToString() + "  " + dt.Rows(0)("dealer_lname").ToString()
        '    '        DealerCity.Text = dt.Rows(0)("dealer_city").ToString()
        '    '    Catch ex As Exception
        '    '        Dealername.Text = "--"
        '    '        DealerCity.Text = "--"


        '    '    End Try
    End Sub
End Class